package com.topisv.tms.web;

import java.util.Date;
import java.util.List;

import javax.validation.Valid;

import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.topisv.tms.entity.Customers;
import com.topisv.tms.entity.TaskNumber;
import com.topisv.tms.service.CustomersService;
import com.topisv.tms.service.TaskNumberService;
import com.topisv.tms.service.account.ShiroDbRealm.ShiroUser;

@Controller
@RequestMapping(value = "/taskNumber")
public class TaskNumberController {

	@Autowired
	private TaskNumberService taskNumberService;
	@Autowired
	private CustomersService customersService;

	@RequestMapping(value = "init")
	public String init(Model model) {
		ShiroUser user =getShiroUser();
		List<Customers> customers=customersService.loadCustomerByAuthority(user);
		model.addAttribute("customers", customers);
		model.addAttribute("status", 0);
		return "taskNumber/taskNumberLists";
	}
	
	@RequestMapping(value = "init/{customerCode}")
	public String initCustomerCode(@PathVariable(value="customerCode") String customerCode,Model model) {
		ShiroUser user =getShiroUser();
		List<Customers> customers=customersService.loadCustomerByAuthority(user);
		TaskNumber singleTaskNumber=taskNumberService.findByTypeAndCustomerCode("single",customerCode);
		TaskNumber multipleTaskNumber=taskNumberService.findByTypeAndCustomerCode("multiple",customerCode);
		TaskNumber forwardTaskNumber=taskNumberService.findByTypeAndCustomerCode("forward",customerCode);
		model.addAttribute("single", singleTaskNumber);
		model.addAttribute("multiple", multipleTaskNumber);
		model.addAttribute("forward", forwardTaskNumber);
		model.addAttribute("customers", customers);
		model.addAttribute("customerCode", customerCode);
		model.addAttribute("status", 1);
		return "taskNumber/taskNumberLists";
	}
	
	@RequestMapping(value = "create", method = RequestMethod.POST)
	public String create(@Valid TaskNumber taskNumber, RedirectAttributes redirectAttributes,Model model) {
		ShiroUser user = getShiroUser();
		TaskNumber tempTaskNumber=taskNumberService.findByTypeAndCustomerCode(taskNumber.getType(),user.customerCode);			
		if(tempTaskNumber!=null){
			taskNumber.setId(tempTaskNumber.getId());
		}
		taskNumber.setUpdateDate(new Date());
		taskNumber.setJobuuid(user.jobuuid);
		taskNumber.setCustomerCode(user.customerCode);
		taskNumberService.save(taskNumber);
		TaskNumber singleTaskNumber=taskNumberService.findByTypeAndCustomerCode("single",user.customerCode);
		TaskNumber multipleTaskNumber=taskNumberService.findByTypeAndCustomerCode("multiple",user.customerCode);
		TaskNumber forwardTaskNumber=taskNumberService.findByTypeAndCustomerCode("forward",user.customerCode);
		model.addAttribute("single", singleTaskNumber);
		model.addAttribute("multiple", multipleTaskNumber);
		model.addAttribute("forward", forwardTaskNumber);
		redirectAttributes.addFlashAttribute("message", "创建功能成功");
		return "taskNumber/taskNumberLists";
	}
	
	/**
	 * 取出Shiro中的当前用户Id.
	 */
	private ShiroUser getShiroUser() {
		ShiroUser user = (ShiroUser) SecurityUtils.getSubject().getPrincipal();
		return user;
	}

}
